package com.songcha.sakura.helper

import com.songcha.sakura.config.DownloadConfig
import com.songcha.sakura.struct.VideoType
import com.songcha.sakura.util.FileUtil
import java.io.File

class VideoHelper {
    interface ILocalVideoListener{
        fun onSearchComicBefore(comicFile:File)
        fun onSearchComicAfter(comicFile:File)
        fun onSearchEpisode(type: VideoType,comicFile:File,episodeFile:File)
    }
    companion object{
        @JvmStatic
        fun getLocalVideoList(listener:ILocalVideoListener){
            //val dataPath=context.filesDir.absolutePath
            val dataPath=DownloadConfig.getVideoDownloadPath()
            val dataFile = File(dataPath)
            val files = dataFile.listFiles()
            for (comicFile in files){
                if(comicFile.isDirectory){
                    listener.onSearchComicBefore(comicFile)
                    //番剧存在
                    if(FileUtil.isDirExist(comicFile)){
                        //集存在
                        val episodeFiles = comicFile.listFiles()
                        //遍历集
                        for (episodeFile in episodeFiles){
                            if(episodeFile.isDirectory){
                                listener.onSearchEpisode(VideoType.M3U8,comicFile,episodeFile)
                            }else{
                                if(episodeFile.name.endsWith(".mp4")){
                                    listener.onSearchEpisode(VideoType.MP4,comicFile,episodeFile)
                                }else if(episodeFile.name.endsWith(".mkv")){
                                    listener.onSearchEpisode(VideoType.MKV,comicFile,episodeFile)
                                }else{
                                    listener.onSearchEpisode(VideoType.UNKNOW,comicFile,episodeFile)
                                }
                            }

                        }

                    }
                    listener.onSearchComicAfter(comicFile)
                }
            }

        }
    }
}